package com.njwb.day18.factory;

public class Factory implements Runnable {

    private Produce produce;

    public Factory(Produce produce) {
        this.produce = produce;
    }
    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (produce) {
                if (produce.count != 0) {
                    try {
                        produce.wait(); // 等待消费
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 模拟生产
                if (count % 2 == 0) {
                    produce.name = "鲁班七号";
                    produce.skin = "电玩小子";
                } else {
                    produce.name = "亚瑟";
                    produce.skin = "死亡骑士";
                }
                produce.count = 1;
                count++;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("生产者生产了: " + produce.name + " (" + produce.skin + ")");
                produce.notifyAll(); // 通知消费者消费
            }
        }
    }
}
